//
//  GuideViewController.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/8/18.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "GuideViewController.h"

@interface GuideViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIButton *goBtn;

@property (nonatomic, strong) UIScrollView *scrollView;
@end

@implementation GuideViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, GHEIGHT)];
    for (int i=0; i<3; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"1242x2208-%d",i+1]];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(GWIDTH * i, 0, GWIDTH, GHEIGHT)];
        // 在最后一页创建按钮
        if (i == 2) {
            // 必须设置用户交互 否则按键无法操作
            imageView.userInteractionEnabled = YES;
            _goBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [_goBtn setTitle:@"立即开启" forState:0];
            [_goBtn setTitleColor:ColorFrom16(0x202020) forState:0];
            _goBtn.titleLabel.font = PFRFONT(16);
            _goBtn.layer.backgroundColor = ColorFrom16(0xFEDE6D).CGColor;
            if(IS_IPHONE5)
            {
                _goBtn.layer.cornerRadius = 17.5;
            }
            else
            {
                _goBtn.layer.cornerRadius = 24.5;
            }
            [_goBtn addTarget:self action:@selector(gotoAction) forControlEvents:UIControlEventTouchUpInside];
            [imageView addSubview:_goBtn];
            [_goBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                if(IS_IPHONE5)
                {
                    make.bottom.equalTo(imageView).offset(-30);
                    make.height.equalTo(@35);
                }
                else if(IS_IPHONE_PLUS)
                {
                    make.bottom.equalTo(imageView).offset(-56);
                    make.height.equalTo(@49);
                }
                else
                {
                    make.bottom.equalTo(imageView).offset(-48);
                    make.height.equalTo(@49);
                }
                make.centerX.equalTo(imageView.mas_centerX);
                make.width.equalTo(@182);
            }];
            
        }
        imageView.image = image;
        [_scrollView addSubview:imageView];
    }
    _scrollView.bounces = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.contentSize = CGSizeMake(GWIDTH * 3, GHEIGHT);
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
}

- (void)gotoAction
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    
    [[NSUserDefaults standardUserDefaults] setObject:APP_VERSION forKey:kUDAppVersion];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
   NSInteger page = (NSInteger)(scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width);
    if(page == 2)
    {
        _goBtn.alpha =1 ;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
